package com.sds.smarthome.foundation.repository.service;

import android.content.Context;

import com.sds.sdk.android.sh.OnPushEventReceivedListener;
import com.sds.sdk.android.sh.OnSHClientStateChangedListener;
import com.sds.sdk.android.sh.SH;
import com.sds.sdk.android.sh.SHClient;
import com.sds.sdk.android.sh.common.SHClientState;
import com.sds.sdk.android.sh.common.SHErrorCode;
import com.sds.sdk.android.sh.model.PushEvent;
import com.sds.smarthome.foundation.SmartHomeApi;
import com.sds.smarthome.foundation.SmartHomeFactoryApi;


public class SmartHomeFactory implements SmartHomeFactoryApi {

    private SmartHomeCallback mCenterCallback;

    public SmartHomeFactory(SmartHomeCallback centerCallback) {
        this.mCenterCallback = centerCallback;
    }

    @Override
    public SmartHomeApi createSmartHome(Context context, String userName, String smartName, String password) {
        SH client = SHClient.newInstance(context, smartName, password, userName, "android_dev", null, clientStateChangedListener);
        client.setOnPushEventReceivedListener(clientPushEventListener);
        client.start();
        return new SmartHomeManager(client);
    }

    private OnSHClientStateChangedListener clientStateChangedListener = new OnSHClientStateChangedListener() {
        @Override
        public void onSHClientStateChanged(String ccuName, SHClientState currentState, SHErrorCode errorCode, boolean isLancon) {
            mCenterCallback.onSmartHomeStateChanged(ccuName, currentState, errorCode, isLancon);
        }
    };

    private OnPushEventReceivedListener clientPushEventListener = new OnPushEventReceivedListener() {
        @Override
        public void onPushEventReceived(String ccuName, PushEvent event) {

            mCenterCallback.onSmartHomeEventReceived(ccuName, event);
        }
    };
}
